<?php
/**
 * RuiNuoCode低代码后台系统 - 团队十多年低代码经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 张家界瑞诺科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.ruinuocrm.com
 * =========================================================
 */
namespace app\admin\service\file;
use app\common\service\Validate as ValidateService;//表单数据验证服务
use app\common\service\Power as PowerService;//表单权限验证服务
use app\common\service\Logic as LogicService;//表单更新服务
use app\common\model\AdminFile as DataMode;//当前模型
use app\admin\service\file\Custom as RuinuoCustom;//自定义方法
use think\facade\Db;
class Logic extends RuinuoCustom
{
    /*新增->获取表单配置->过滤表单参数->验证表单->验证字段权限->新增完成*/
    public function Add($params = [])
    {
        $Model = new DataMode();
        $LogicService = new LogicService();
        $ValidateService = new ValidateService();
        $PowerService = new PowerService();
        $check = $ValidateService->Rule($this->GetVerify(),$params,0);
        if ($check['code'] == '203') {
            return $check;
        }
        $formData =$this->GetFormData($params);
        $ItemData=$PowerService->FormField($formData,'admin:file','add');
        $ItemData = $this->savebefore($ItemData);//保存前处理
        if (!empty($ItemData['code'])) {
            return ['code' => 203, 'msg' => $ItemData['msg'],'data'=>[]];
        }
        $back['id']=$LogicService->Curd('add','admin_file',0,$ItemData,$this->GetDataBase());//执行新增操作
        $saveafter = $this->saveafter($back['id']);//保存后处理
        if (!empty($saveafter['code'])) {
            return ['code' => 203, 'msg' => $saveafter['msg'],'data'=>[]];
        }
        return ['code' => 200, 'msg' => '新增成功', 'data' => $back];
    }

    /*修改->获取表单配置->过滤表单参数->验证表单->验证字段权限->修改完成*/
    public function Edit($params = [])
    {
        if (empty($params['id'])) {
            return ['code' => 203, 'msg' => '参数ID不能为空', 'data' => []];
        }
        $Model = new DataMode();
        $LogicService = new LogicService();
        $ValidateService = new ValidateService();
        $PowerService = new PowerService();
        $check = $ValidateService->Rule($this->GetVerify(),$params,$params['id']);
        if ($check['code'] == '203') {
            return $check;
        }
        $where="a.id='" . $params['id'] . "'";
        $where.=$PowerService->IndexWhere('admin:file');
        $find = $Model->alias('a')->where($where)->count();
        if ($find == 0) {
            return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
        }
        $formData =$this->GetFormData($params);
        $ItemData=$PowerService->FormField($formData,'admin:file','edit');
        $ItemData = $this->savebefore($ItemData,$params['id']); //保存前处理
        if (!empty($ItemData['code'])) {
            return ['code' => 203, 'msg' => $ItemData['msg'],'data'=>[]];
        }
        $back['id']=$LogicService->Curd('edit','admin_file',$params['id'],$ItemData,$this->GetDataBase());//执行修改操作
        $saveafter = $this->saveafter($back['id']);//保存后处理
        if (!empty($saveafter['code'])) {
            return ['code' => 203, 'msg' => $saveafter['msg'],'data'=>[]];
        }
        return ['code' => 200, 'msg' => '修改成功', 'data' => $back];
    }

    /*删除以及批量删除*/
    public function Delete($del_id=0,$type=1)
    {
        if (empty($del_id)) {
            return ['code' => 203, 'msg' => '参数不能为空', 'data' => []];
        }
        $delbefore = $this->delbefore($del_id);//删除前处理
        if (!empty($delbefore['code'])) {
            return ['code' => 203, 'msg' => $delbefore['msg'],'data'=>[]];
        }
        $Model = new DataMode();
        $LogicService = new LogicService();
        $PowerService = new PowerService();
        $where="a.id='".$del_id."'";
        if ($type==2){
            $where="a.id in(".$del_id.")";
        }
        $where.=$PowerService->IndexWhere('admin:file');
        $find = $Model->alias('a')->where($where)->field("a.id")->select()->toArray();
        if (empty($find)) {
            return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
        }
        $del_ids='';
        foreach ($find as $k => $v) {
            $del_ids.=$v['id'].',';
        }
        $del_ids=rtrim($del_ids, ",");
        $LogicService->Curd('delete','admin_file',$del_ids);//执行删除操作
        $delafter = $this->delafter($del_ids);//删除后处理
        if (!empty($delafter['code'])) {
            return ['code' => 203, 'msg' => $delafter['msg'],'data'=>[]];
        }
        return ['code' => 200, 'msg' => '删除成功','data'=>[]];
    }
    /*查看->获取表单配置->过滤表单参数->验证字段查看权限->查看完成*/
    public function Detail($params = [])
    {
        $Model = new DataMode();
        $LogicService = new LogicService();
        $PowerService = new PowerService();
        $field='a.name,a.size,a.mime,a.url,a.source_url,a.id,a.create_time,a.update_time,a.create_id,a.update_id';
        $find = [];
        if (!empty($params['id'])) {
            $where="a.id='" . $params['id'] . "'";
            $where.=$PowerService->IndexWhere('admin:file');
            $find = $Model->alias('a')->where($where)->field($field)->findOrEmpty()->toArray();
            if (empty($find)) {
                return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
            }

        }
        $node=!empty($params['node']) ? $params['node'] : 'detail';
        $power=$PowerService->FormField($find,'admin:file','detail',$node);//是否可见权限
        $data['detail']=$power['detail'];
        $data['power']=$power['power'];
        $source='';
        $data['source'] = $LogicService->GetSource($source);
        $data = $this->oneafter($data);//查询后处理
        if (!empty($data['code'])) {
            return ['code' => 203, 'msg' => $data['msg'], 'data' => []];
        }
        return ['code' => 200, 'msg' => '请求成功', 'data' => $data];
    }

    /*列表字段修改*/
    public function UpdateField($params = [])
    {
        if (empty($params['id'])) {
            return ['code' => 203, 'msg' => '参数id不能为空', 'data' => []];
        }
        if (empty($params['field'])) {
            return ['code' => 203, 'msg' => '参数field不能为空', 'data' => []];
        }
        $Model = new DataMode();
        $LogicService = new LogicService();
        $params = $this->tablebefore($params);//修改前处理
        if (!empty($params['code'])) {
            return ['code' => 203, 'msg' => $params['msg'], 'data' => []];
        }
        $LogicService->Curd('update_field','admin_file',$params['id'],$params,$this->GetDataBase());//执行字段修改操作
        $tableafter = $this->tableafter($params);//修改后处理
        if (!empty($tableafter['code'])) {
            return ['code' => 203, 'msg' => $tableafter['msg'],'data'=>[]];
        }
        return ['code' => 200, 'msg' =>'修改成功','data'=>[]];;
    }

    /*获取表单参数*/
    public function GetFormData($params = [])
    {
        $LogicService = new LogicService();
        if (isset($params['name'])){
            $data['a']['name']=strlen($params['name']) ? $params['name'] : NULL;
        }
        if (isset($params['size'])){
            $data['a']['size']=strlen($params['size']) ? $params['size'] : NULL;
        }
        if (isset($params['mime'])){
            $data['a']['mime']=strlen($params['mime']) ? $params['mime'] : NULL;
        }
        if (isset($params['url'])){
            $data['a']['url']=strlen($params['url']) ? $params['url'] : NULL;
        }
        if (isset($params['source_url'])){
            $data['a']['source_url']=strlen($params['source_url']) ? $params['source_url'] : NULL;
        }

        return $data;
    }

    /*获取表单验证参数*/
    public function GetVerify()
    {
        $data=[];
        return $data;
    }

    /*关联更新需要用到的数据表*/
    public function GetDataBase()
    {
        $data=[];
        return $data;
    }


}